/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.News;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.service.NewsPcService;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.cmcc.zysoft.sysmanage.service.SystemUserPCService;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: NewsController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-13 上午9:29:45
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("pc/news")
public class NewsController extends BaseController {

	/**
	 * 皮肤包上传路径.
	 */
	@Value("${upload.file.path}")
	private String path;
	
	/**
	 * 属性名称: newsPcService.
	 * 类       型: NewsPcService.
	 */
	@Resource
	private NewsPcService newsPcService;
	
	/**
	 * 属性名称: systemUserPcService.
	 * 类       型: SystemUserPcService.
	 */
	@Resource
	private SystemUserPCService systemUserPcService;
	
	/**
	 * 新闻列表页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/news/list";
	}
	
	/**
	 * 获取新闻分页列表信息.
	 * @param keyword 搜索关键字
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return String, Object>
	 */
	@RequestMapping("/list.htm")
	@ResponseBody
	public Map<String, Object> getNewsList(String keyword, int page, int rows) {
		return this.newsPcService.getNewsList(keyword, page, rows);
	}
	
	/**
	 * 添加新闻页面.
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add() {
		return "/pc/news/add";
	}
	
	/**
	 * 保存新闻信息.
	 * @param news 通知公告对象
	 * @return String
	 */
	@RequestMapping("/saveNewsInfo.htm")
	@ResponseBody
	@Transactional
	public String saveNewsInfo(News news) {
		User user = SecurityContextUtil.getCurrentUser();
		news.setPublishMan(user.getUserId());
		news.setPublishTime(new Date());
		news.setStatus("0");
		news.setNewsType("0"); //类型:0-news,1-message
		if(StringUtils.hasText(news.getNewsId())) {
			this.newsPcService.updateEntity(news);
		} else {
			this.newsPcService.insertEntity(news);
		}
		return "SUCCESS";
	}
	
	/**
	 * 编辑新闻信息页面.
	 * @param modelMap modelMap
	 * @param newsId 新闻ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String edit(ModelMap modelMap, String newsId) {
		News news = this.newsPcService.getEntity(newsId);
		modelMap.addAttribute("news", news);
		return "/pc/news/edit";
	}
	
	/**
	 * 新闻详情页面.
	 * @param modelMap modelMap
	 * @param newsId 新闻ID
	 * @return String
	 */
	@RequestMapping("/detail.htm")
	public String detail(ModelMap modelMap, String newsId) {
		News news = this.newsPcService.getEntity(newsId);
		SystemUser systemUser = this.systemUserPcService.getEntity(news.getPublishMan());
		modelMap.addAttribute("news", news);
		modelMap.addAttribute("userName", systemUser.getRealName());
		return "/pc/news/detail";
	}
	
	/**
	 * 发布新闻(状态:草稿->发布).
	 * @param newsId 新闻ID
	 * @return map
	 */
	@RequestMapping("/publish.htm")
	@ResponseBody
	@Transactional
	public Map<String, Object> saveNewsInfo(String newsId) {
		Map<String, Object> map = new HashMap<String, Object>();
		News news = this.newsPcService.getEntity(newsId);
		news.setState("1");
		this.newsPcService.updateEntity(news);
		map.put("code", "0");
		return map;
	}
	
	/**
	 * 发布新闻(状态:草稿->发布).
	 * @param newsIds 新闻ID集合
	 * @return  Map<String, Object>
	 */
	@RequestMapping("/remove.htm")
	@ResponseBody
	@Transactional
	public Map<String, Object> remove(String newsIds) {
		News news = null;
		String[] array = newsIds.split(",");
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i = 0; i < array.length; i++) {
			news = this.newsPcService.getEntity(array[i]);
			news.setStatus("1");
			this.newsPcService.updateEntity(news);
		}
		map.put("code", "0");
		return map;
	}
	
	/**
	 * 检查新闻标题是否已经存在.
	 * @param title 新闻标题
	 * @param newsId 新闻ID
	 * @return map
	 */
	@RequestMapping("/check.htm")
	@ResponseBody
	public Map<String, Object> check(String title, String newsId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.newsPcService.check(title, newsId);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
	
	/**
	 * @Description : KindEditor上传图片专用.
	 * @param imgFile 图片文件
	 * @param request Http请求
	 * @return String 成功标识、图片地址
	 */
	@RequestMapping("/uploadImage.htm")
	@ResponseBody
	public String uploadImage(@RequestParam("imgFile") MultipartFile imgFile, HttpServletRequest request) {
		JSONObject result = new JSONObject();
		try {
			if(null != imgFile && !imgFile.isEmpty()) {
				String orginFileName = imgFile.getOriginalFilename();
				String realPath = request.getSession().getServletContext().getRealPath("/resources/image/upload");
				File file = new File(realPath);
				if (!file.exists()) {
					file.mkdirs();
				}
				file.setWritable(true);
				String fileName = (String.valueOf(System.currentTimeMillis())) + orginFileName.substring(orginFileName.indexOf("."));
				String path = realPath + File.separator + fileName;
				File destFile = new File(path);
				FileUtils.copyInputStreamToFile(imgFile.getInputStream(), destFile);
				path = "/resources/image/upload/" + fileName;
				result.put("error", 0);
				result.put("url", path);
			}
		} catch (Exception e) {
			result.put("error", 1);
		}
		return result.toJSONString();
	}
}